﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;
using Dict.Blo;
using Dict.DataModel;
using Dict;
using ProtoBuf.Message;
namespace UI
{
    public class UIFieldHpSetPanel : MonoBehaviour
    {
        public TextureKit iconOne;
        public TextureKit iconTwo;
        public UILabel numOne;
        public UILabel numTwo;
        public UILabel LCD;
        public UILabel RCD;

        public GameObject leftAddBtn;
        public GameObject rightAddBtn;
        public GameObject leftMask;
        public GameObject rightMask;
        Dictionary<int, string> hpItems = new Dictionary<int, string>();
        string leftId;
        string rightId;
        int leftType = 0;
        int rightType = 0;

        float cdTimes = 0f;
        bool cdFlag = false;
        //List<BaseResource> resOne = new List<BaseResource>();
        //List<BaseResource> resTwo = new List<BaseResource>();
        // Use this for initialization
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            //long current = ServerTimerTool.CurrentUTCJavaTime();
            if (cdFlag)
            {
                LCD.gameObject.SetActive(true);
                RCD.gameObject.SetActive(true);
                leftMask.SetActive(true);
                rightMask.SetActive(true);
                cdTimes -= Time.deltaTime;
                string cd = string.Format("{0}", (int)cdTimes);
                LCD.text = cd;
                RCD.text = cd;
                if (cdTimes <= 0)
                {
                    cdFlag = false;
                    LCD.gameObject.SetActive(false);
                    RCD.gameObject.SetActive(false);
                    leftMask.SetActive(false);
                    rightMask.SetActive(false);

                }
            }

        }
        public void Refresh()
        {
            CSGemAndCookingList gcmsg = new CSGemAndCookingList();
            NetAPI.SendHttp(OpDefine.CSGemAndCookingList, gcmsg, GCListCallback);
        }

        public void setHpItems()
        {
            //判断是否进入CD
            //CSGemAndCookingList gcmsg = new CSGemAndCookingList();
            //NetAPI.SendHttp(OpDefine.CSGemAndCookingList, gcmsg, GCListCallback);
            Refresh();
            LCD.gameObject.SetActive(false);
            RCD.gameObject.SetActive(false);
            leftMask.SetActive(false);
            rightMask.SetActive(false);
            hpItems = StaticData.playerData.setHpItems;
            if(hpItems[0] != "")
            {
                leftAddBtn.gameObject.SetActive(false);
                iconOne.gameObject.SetActive(true);
                numOne.gameObject.SetActive(true);
                string[] sArr = hpItems[0].Split('_');
                int isItem = 0;
                leftId = hpItems[0];
                if (sArr[0] == "FishItem")
                {
                    isItem = 1;
                    iconOne.iconType = TextureIcon.FISH;
                    //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(hpItems[0]);
                    iconOne.textureName = hpItems[0];
                    numOne.text = StaticData.playerData.FishItems[hpItems[0]].num + "";
                    leftType = 1;
                }
                if(isItem == 0)
                {
                    iconOne.iconType = TextureIcon.ITEM;
                    var ti = DictDataManager.Instance.tableItemDao.GetById(hpItems[0]);
                    iconOne.textureName = ti.icon;
                    numOne.text = StaticData.playerData.GetSimpleGoods(hpItems[0]).num + "";
                    leftType = 0;
                }
                
            }
            else
            {
                leftAddBtn.gameObject.SetActive(true);
                iconOne.gameObject.SetActive(false);
                numOne.gameObject.SetActive(false);
                leftId = "";
                iconOne.iconType = TextureIcon.ITEM;
                leftType = 0;
            }

            if (hpItems[1] != "")
            {
                rightAddBtn.gameObject.SetActive(false);
                iconTwo.gameObject.SetActive(true);
                numTwo.gameObject.SetActive(true);
                string[] sArr = hpItems[1].Split('_');
                int isItem = 0;
                rightId = hpItems[1];
                if (sArr[0] == "FishItem")
                {
                    isItem = 1;
                    iconTwo.iconType = TextureIcon.FISH;
                    //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(hpItems[1]);
                    iconTwo.textureName = hpItems[1];
                    numTwo.text = StaticData.playerData.FishItems[hpItems[1]].num + "";
                    rightType = 1;
                }
                if (isItem == 0)
                {
                    iconTwo.iconType = TextureIcon.ITEM;
                    var ti = DictDataManager.Instance.tableItemDao.GetById(hpItems[1]);
                    iconTwo.textureName = ti.icon;
                    numTwo.text = StaticData.playerData.GetSimpleGoods(hpItems[1]).num + "";
                    rightType = 0;
                }  
            }
            else
            {
                rightAddBtn.SetActive(true);
                iconTwo.gameObject.SetActive(false);
                numTwo.gameObject.SetActive(false);
                rightId = "";
                iconTwo.iconType = TextureIcon.ITEM;
                rightType = 0;
            }
        }

        void GCListCallback(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCGemAndCookingList cookingList = packet.kBody as SCGemAndCookingList;
                long endTime = cookingList.nextUseTime;
                long current = ServerTimerTool.CurrentUTCJavaTime();
                if (current > endTime)
                {
                    cdFlag = false;
                    cdTimes = 0f;
                    LCD.gameObject.SetActive(false);
                    RCD.gameObject.SetActive(false);
                    leftMask.SetActive(false);
                    rightMask.SetActive(false);
                }
                else
                {
                    cdTimes = (float)(endTime - current) / 1000;
                    cdFlag = true;
                }
            }
        }

        public void setLBtnData(string id,string num,int type)
        {
            if(id == "")
            {
                return;
            }
            leftAddBtn.SetActive(false);
            iconOne.gameObject.SetActive(true);
            numOne.gameObject.SetActive(true);
            leftId = id;
            leftType = type;
            if(type == 0)
            {
                iconOne.iconType = TextureIcon.ITEM;
                var ti = DictDataManager.Instance.tableItemDao.GetById(id);
                iconOne.textureName = ti.icon;
                numOne.text = StaticData.playerData.GetSimpleGoods(id).num+"";
            }
            else
            {
                iconOne.iconType = TextureIcon.FISH;
                //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(id);
                iconOne.textureName = id;
                numOne.text = StaticData.playerData.FishItems[id].num + "";
            }
            
        }
        public void setRBtnData(string id, string num,int type)
        {
            if (id == "")
            {
                return;
            }
            rightAddBtn.SetActive(false);
            iconTwo.gameObject.SetActive(true);
            numTwo.gameObject.SetActive(true);
            rightId = id;
            rightType = type;
            if (type == 0)
            {
                iconTwo.iconType = TextureIcon.ITEM;
                var ti = DictDataManager.Instance.tableItemDao.GetById(id);
                iconTwo.textureName = ti.icon;
                numTwo.text = StaticData.playerData.GetSimpleGoods(id).num + "";
            }
            else
            {
                iconTwo.iconType = TextureIcon.FISH;
                //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(id);
                iconTwo.textureName = id;
                numTwo.text = StaticData.playerData.FishItems[id].num + "";
            }
        }

        public void onClickLeft()
        {
            if (cdFlag)
            {
                string tip = TableTextBlo.GetText("field_hp_cd_tip");
                BoxManager.CreatePopupTextBox(tip);
                return;
            }
            string num = numOne.text;
            if(int.Parse(num)<=0)
            {
                string tip = TableTextBlo.GetText("field_hp_zero_tip");
                BoxManager.CreatePopupTextBox(tip);
                return;
            }
            CSHpRecover msg = new CSHpRecover();

            BaseResource res = new BaseResource();
            res.id = leftId;
            res.guid = "";
            res.num = 1;
            msg.costItem = res;
            NetAPI.RegistSocketHandler(OpDefine.SCHpRecover, HpRecover);
            NetAPI.SendSocket(OpDefine.CSHpRecover, msg);
            //NetAPI.SendHttp(OpDefine.CSHpRecover, msg, HpCallBack);
        }
        void HpRecover(object obj)
        {
            NetAPI.UnregisterSocketHandler(OpDefine.SCHpRecover, HpRecover);
            //Debug.Log("Left recover"+ StaticData.playerData.GetSimpleGoods(leftId).num);
            SCHpRecover pos = (SCHpRecover)obj;
            StaticData.playerData.Delete(pos.cost);
            if (leftType == 0)
            {
                numOne.text = StaticData.playerData.GetSimpleGoods(leftId).num + "";
            }
            else if (leftType == 1)
            {
                numOne.text = StaticData.playerData.FishItems[leftId].num + "";
            }
            cdTimes = 60f;
            cdFlag = true;
        }
        void HpCallBack(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCHpRecover msg = packet.kBody as SCHpRecover;
                StaticData.playerData.Delete(msg.cost);
                if(leftType == 0)
                {
                    numOne.text = StaticData.playerData.GetSimpleGoods(leftId).num + "";
                }
                else if(leftType == 1)
                {
                    numOne.text = StaticData.playerData.FishItems[leftId].num + "";
                }
            }
        }

        public void onClickRight()
        {
            if (cdFlag)
            {
                string tip = TableTextBlo.GetText("field_hp_cd_tip");
                BoxManager.CreatePopupTextBox(tip);
                return;
            }
            string num = numTwo.text;
            if (int.Parse(num) <= 0)
            {
                string tip = TableTextBlo.GetText("field_hp_zero_tip");
                BoxManager.CreatePopupTextBox(tip);
                return;
            }
            CSHpRecover msg = new CSHpRecover();
            BaseResource res = new BaseResource();
            res.id = rightId;
            res.guid = "";
            res.num = 1;
            msg.costItem = res;
            NetAPI.RegistSocketHandler(OpDefine.SCHpRecover, SCHpRecover);
            NetAPI.SendSocket(OpDefine.CSHpRecover, msg);
            //cdTimes = 60f;
            //cdFlag = true;
            //NetAPI.SendHttp(OpDefine.CSHpRecover, msg, HpRightCallBack);
        }
        void SCHpRecover(object obj)
        {
            NetAPI.UnregisterSocketHandler(OpDefine.SCHpRecover, SCHpRecover);
            //Debug.Log("right recover" + StaticData.playerData.GetSimpleGoods(rightId).num);
            SCHpRecover pos = (SCHpRecover)obj;
            StaticData.playerData.Delete(pos.cost);
            if (rightType == 0)
            {
                numTwo.text = StaticData.playerData.GetSimpleGoods(rightId).num + "";
            }
            else if (rightType == 1)
            {
                numTwo.text = StaticData.playerData.FishItems[rightId].num + "";
            }
            cdTimes = 60f;
            cdFlag = true;
        }
        void HpRightCallBack(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCHpRecover msg = packet.kBody as SCHpRecover;
                StaticData.playerData.Delete(msg.cost);
                if(rightType == 0)
                {
                    numTwo.text = StaticData.playerData.GetSimpleGoods(rightId).num + "";
                }
                else if(rightType == 1)
                {
                    numTwo.text = StaticData.playerData.FishItems[rightId].num + "";
                }
            }
        }
    }
}

